package cas.ihep.test;

import com.google.common.io.Closer;

import java.io.Closeable;
import java.io.IOException;
import java.sql.*;

public class Lua {

    static class AutoCloseable2Closeable<T extends AutoCloseable> implements Closeable{
        T instance;

        AutoCloseable2Closeable(T obj){
            instance=obj;
        }

        public void close()throws IOException{
            try {
                instance.close();
            }catch (RuntimeException | IOException e){
                throw e;
            } catch (Exception e) {
                throw new IOException(e);
            }
        }
    }
    private static final String url = "jdbc:postgresql://helion01.ihep.ac.cn:5432/postgres";

    private static final String usr = "postgres";

    private static final String psd = "postgres";

    public static void main(String [] args) throws Exception {
        Class.forName("org.postgresql.Driver");
        try(Closer closer=Closer.create()){
            Connection conn= closer.register(new AutoCloseable2Closeable<>(DriverManager.getConnection(url,usr,psd))).instance;
            Statement stmt=closer.register(new AutoCloseable2Closeable<>(conn.createStatement())).instance;
            String sql="select * from test_array;";
            ResultSet rs=closer.register(new AutoCloseable2Closeable<>(stmt.executeQuery(sql))).instance;
            if(rs.next()){
                Array arr=rs.getArray("items");
                String[] tmp=(String[])arr.getArray(1,0);
                for(String val:tmp){
                    System.out.println(val);
                }
            }
        }
    }

}
